'''
Created on Jun 12, 2009

@author: tdelev
'''
from django.shortcuts import *
from django.http import *
from datetime import datetime
from forms import *
from model.models import *

SORT_MODE = ['newest', 'featured'] 

def index(request):
    sort = SORT_MODE[0]
    if 'sort' in request.GET:
        sort = request.GET['sort']
    questions = []
    if sort in SORT_MODE:
        if sort == SORT_MODE[0]:
            questions = Question.get_all('date_posted')
        if sort == SORT_MODE[1]:
            questions = Question.get_featured()
    else:       
        questions = Question.get_all('date_posted')
    
    return render_to_response('questions/questions.html', { 'questions' : questions })

def unanswered(request):       
    questions = Question.get_unanswered() 
    return render_to_response('questions/unanswered.html', { 'questions' : questions })

def tagged(request, tag):      
    questions = Question.get_by_tag(tag) 
    return render_to_response('questions/tagged.html', { 'questions' : questions })

def question(request, id):       
    question = Question.get_by_id(int(id)) 
    return render_to_response('questions/question.html', { 'q' : question })


def ask(request):
    if request.method == 'POST':      
        title = request.POST['title']
        description = request.POST['post-text']
        tags = request.POST['tagnames']
        tags = tags.strip().split(' ')
        q = Question(title = title, description = description, tags = tags)
        q.save()
        return HttpResponseRedirect("/questions/")
    
    return render_to_response("questions/ask.html")    
    
def ticks(request):
    return HttpResponse('NGn1L8')    